package com.yiwei.zhh.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.activity.Application;


/**
 * Created by Master.Dong on 2016/4/5 0005.
 */
public class ToastUtil {
    private static Toast mToast;
    private static TextView textView;

    /**
     * @param context  上下文
     * @param text     内容
     * @param duration 时长
     * @return
     */
    public static Toast makeText(Context context, CharSequence text,
                                 int duration) {
//        Toast toast = new Toast(Application.getInstance());
//
//        // 获取LayoutInflater对象
//        LayoutInflater inflater = (LayoutInflater) Application.getInstance()
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        // 由layout文件创建一个View对象
//        View layout = inflater.inflate(R.layout.toast, null);
//
//        TextView textView = (TextView) layout.findViewById(R.id.text);
//        textView.setText(text);
//
//        toast.setView(layout);
//        toast.setGravity(Gravity.BOTTOM, 0, 200);
//        toast.setDuration(duration);
        show(text);
        return mToast;
    }


    public static Toast makeText(Context context, CharSequence text) {
//        Toast toast = new Toast(Application.getInstance());
//
//        // 获取LayoutInflater对象
//        LayoutInflater inflater = (LayoutInflater) Application.getInstance()
//                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        // 由layout文件创建一个View对象
//        View layout = inflater.inflate(R.layout.toast, null);
//
//        TextView textView = (TextView) layout.findViewById(R.id.text);
//        textView.setText(text);
//
//        toast.setView(layout);
//        toast.setGravity(Gravity.BOTTOM, 0, 200);
//        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.show();
        show(text);
        return mToast;
    }


    @SuppressLint("ShowToast")
    private static void show(CharSequence text) {
        if (mToast != null) {
            textView.setText(text);
        } else {
            mToast = new Toast(Application.getInstance());
//            LayoutInflater inflater = (LayoutInflater) Application.getInstance()
//                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LayoutInflater inflater = (LayoutInflater) Application.getInstance()
                    .getSystemService("layout_inflater");
            // 由layout文件创建一个View对象
            View layout = inflater.inflate(R.layout.toast, null);
            textView = (TextView) layout.findViewById(R.id.text);
            textView.setText(text);

            mToast.setView(layout);
            mToast.setGravity(Gravity.BOTTOM, 0, 200);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }


    /**
     * @param context  上下文
     * @param text     内容
     * @param duration 时长
     * @return
     */
    public static Toast makeText(Context context, CharSequence text,
                                 int duration, int x, int y) {
        Toast toast = new Toast(context);

        // 获取LayoutInflater对象
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 由layout文件创建一个View对象
        View layout = inflater.inflate(R.layout.toast, null);

        TextView textView = (TextView) layout.findViewById(R.id.text);
        textView.setText(text);

        toast.setView(layout);
        toast.setGravity(Gravity.CENTER, x, y);
        toast.setDuration(duration);

        return toast;
    }


    /**
     * @param context  上下文
     * @param resId    图片资源
     * @param text     内容
     * @param duration 时长
     * @return
     */
    public static Toast makeText(Context context, int resId, CharSequence text,
                                 int duration) {
        Toast toast = new Toast(context);

        // 获取LayoutInflater对象
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 由layout文件创建一个View对象
        View layout = inflater.inflate(R.layout.toast, null);

        ImageView imageView = (ImageView) layout.findViewById(R.id.image);
        TextView textView = (TextView) layout.findViewById(R.id.text);

        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(resId);
        textView.setText(text);

        toast.setView(layout);
        toast.setGravity(Gravity.BOTTOM, 0, 200);
        toast.setDuration(duration);

        return toast;
    }
}
